﻿@using hc.epm.DataModel.Basic;
@using hc.epm.ViewModel;
@model IEnumerable<UserView>
@{
    ViewBag.Title = "请选择用户";
    Layout = "~/Views/Shared/_LayoutDialog.cshtml";
}
<style type="text/css">
    .contentData {
        padding: 0px 20px;
    }

    /*.form-select {
            margin-bottom: 20px;
             margin: 0 10px;
            float: right;
        }*/

    .datalist th {
        height: 40px;
    }

    .datalist td {
        height: 36px;
    }

    .form-select > form > div input {
        width: 160px;
    }

    .form-select > form > div div {
        margin: 0 0 !important;
        width: auto !important;
        float: right !important;
        margin-left: 10px !important;
    }

    .form-select > form > div {
        height: 10%;
        width: auto !important;
        float: left;
    }

    .userNameWrap {
        margin: 10px 0;
        min-height: 25px;
    }

    #showSelectUserName {
        line-height: 28px;
        color: #000000;
    }
</style>
<div class="form-select c">
    <form id="searchForm" method="get">
        <div>
            <div style="width:auto;">
                <label>姓名 : </label><input type="text" name="userName" value="@ViewBag.userName">
                <label>电话 : </label><input type="text" name="phone" value="@ViewBag.phone">
                <label>部门 : </label><input type="text" name="dempName" value="@ViewBag.dempName">

                <input type="hidden" name="selectType" value="@ViewBag.SelectType" />
                <input type="hidden" name="companyId" value="@ViewBag.companyId" />

                <label>岗位 : </label><input type="text" name="postName" value="@ViewBag.postName">
                <div class="form-sub" style="margin-left:10px;">
                    <button type="submit" style="cursor:pointer;">查 询</button>
                </div>
            </div>

        </div>
    </form>
</div>
<br /><br />
<div class="userNameWrap">
    <label id="showSelectUserName"></label>
</div>
<div>
    <table class="datalist">
        <thead>
            <tr>
                <th width="60">
                    <input type="checkbox" id="selectAll" />
                </th>
                <th width="120">序号</th>
                <th>姓名</th>
                <th>电话</th>
                <th>部门</th>
                <th>岗位</th>
                <th>职务</th>
                <th>职称</th>
            </tr>
        </thead>
        <tbody id="tbody">
            @{
                int index = (ViewBag.pageIndex - (ViewBag.pageIndex - 1)) * ((ViewBag.pageIndex - 1) * 10);
            }
            @if (Model != null && Model.Count() > 0)
            {
                foreach (var item in Model)
                {
                    index++;
                    <tr>
                        <td>
                            <input type="checkbox" name="checkbox" value="@item.UserId" data-name="@item.UserName" data-phone="@item.Phone" data-post="@item.PostValue" />
                        </td>
                        <td>@index</td>
                        <td>@item.UserName</td>
                        <td>@item.Phone</td>
                        <td>@item.DepartmentName</td>
                        <td>@item.PostValue</td>
                        <td>@item.ProfessionalQualificationValue</td>
                        <td>@item.ProfessionalValue</td>
                    </tr>
                }
            }
            else
            {
                <tr>
                    <td colspan="8" class="table-center">
                        暂无数据
                    </td>
                </tr>
            }
        </tbody>
    </table>
</div>
@section scripts
    {
    <script type="text/javascript">
        layui.use(['layer', 'element', 'laypage', 'form', 'table'], function () {
            var layer = layui.layer, form = layui.form, element = layui.element, table = layui.table, laypage = layui.laypage;

            //分页
            laypage.render({
                elem: 'page',
                count: '@ViewBag.Total',
                curr: '@ViewBag.pageIndex',
                layout: ['prev', 'page', 'next', 'skip', 'count'],
                theme: '#6a6969',
                jump: function (obj, first) {
                    if (!first) {
                        //得到了当前页，用于向服务端请求对应数据
                        var curr = obj.curr;
                        var search = $("form").serialize();
                        window.location.href = "/Comm/SelectUser?pageIndex=" + curr + "&" + search;
                    }
                }
            });

        });
        if (@ViewBag.selectType == "2")
        {
      
            setSelectUsers();

            $('#tbody :checkbox[name="checkbox"]').change(function (index) {
                var obj = {};
                var IsChecked = $(this).is(':checked');
                obj.id = $(this).val() || '';
                obj.name = $(this).data('name') || '';
                parent.getUserData(IsChecked, obj);
                var selectUserIds = parent.getSelectedUserData();
                if (selectUserIds.userNames !== "") {
                    $('#showSelectUserName').html("已选中：" + selectUserIds.userNames);
                }
                else {
                    $('#showSelectUserName').html(selectUserIds.userNames);
                }
            });
        }


        //全选
        $("#selectAll").click(function () {
            var isChecked = $("#selectAll").prop("checked");
            $("#tbody input[type='checkbox']").prop("checked", isChecked);
            $('#tbody :checkbox[name="checkbox"]').change();
        });
        // 单独选项控制全选
        $("#tbody input[type='checkbox']").click(function () {
            var allLength = $("#tbody input[type='checkbox']").length;
            var checkedLength = $("#tbody input[type='checkbox']:checked").length;
            if (allLength == checkedLength) {
                $("#selectAll").prop("checked", true);
                $('#tbody :checkbox[name="checkbox"]').change();
            } else {
                $("#selectAll").prop("checked", false);
                $('#tbody :checkbox[name="checkbox"]').change();
            }
        });

        function setSelectUsers() {
            var selectUserIds = parent.getSelectedUserData();
            $.each($(':checkbox[name="checkbox"]'), function (index, item) {
                if (selectUserIds.userIds.indexOf(item.value) !== -1) {
                    item.checked = true;
                }
            })
            if (selectUserIds.userNames !== "") {
                $('#showSelectUserName').html("已选中：" + selectUserIds.userNames);
            }
            else {
                $('#showSelectUserName').html(selectUserIds.userNames);
            }
        }



        function getSelectData() {
            var result = {
                flag: true,
                msg: '',
                data: []
            };
            $('#tbody :checkbox[name="checkbox"]:checked').each(function (index) {
                result.data.push({
                    Id: $(this).val() || '',
                    Name: $(this).data('name') || '',
                    Phone: $(this).data('phone') || '',
                    PostValue: $(this).data('post') || ''
                });
            });

            if (result.data.length === 0) {
                result.flag = false;
                result.msg = '请选择用户！';
            }
            return result;
        }
    </script>
}